package com.edu.service.impl;

import com.edu.component.PageBean;
import com.edu.component.QueryParam;
import com.edu.dao.StudentMapper;
import com.edu.entity.Question;
import com.edu.entity.QuestionOptions;
import com.edu.entity.Student;
import com.edu.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageBean listPage(QueryParam param) {
        //{1}开启分页拦截的功能
        Page retPage = PageHelper.startPage(param.getPage(),
                param.getLimit(), true);
        //{2}执行查询.[学生完成]
        studentMapper.queryList( param.getParam() );
        //{3}把 Page 装载进 PageBean 并返回。
        return new PageBean( retPage );
    }
    @Override
    public void regStu(Student student) {
        int updateCnt = studentMapper.addStudent(student);
        if ( updateCnt != 1 ){
            throw new RuntimeException("学生注册失败.");
        }
    }
    @Override
    public Student login(Student student) {
        Student dbStu = studentMapper.getStudentByName(
                student.getUsername()
        );
        //1.如果为空，说明学生不存在
        if ( dbStu == null ){
            throw new RuntimeException("学生登录失败.");
        }
        String ps = dbStu.getPassword();
        if ( !student.getPassword().equals(ps) ){
            throw new RuntimeException("学生登录失败.");
        }
        return dbStu;
    }
}




